package com.zhida.jiyangcenter.widget

import android.content.Context
import android.util.AttributeSet
import android.widget.FrameLayout
import androidx.annotation.AttrRes

class SquareFrameLayout @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr: Int = 0) : FrameLayout(context, attrs, defStyleAttr) {


    /**
     * Measure the view and its content to determine the measured width and the measured height.
     *
     * @param widthMeasureSpec  :  horizontal space requirements as imposed by the parent
     * @param heightMeasureSpec :  vertical space requirements as imposed by the parent
     */
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        //provide a square Preview in xml (not possible in certain cases)
//        if (isInEditMode) {
//            // if height < width : call super method with heightSpec for all sides
//            if (abs(heightMeasureSpec) < abs(widthMeasureSpec)) {
//                super.onMeasure(heightMeasureSpec, heightMeasureSpec)
//            } else {
//                super.onMeasure(widthMeasureSpec, widthMeasureSpec)
//            }// if width <= height : call super method with widthSpec for all sides
//        }
        super.onMeasure(widthMeasureSpec, widthMeasureSpec)

//        init()
    }

    /**
     * Callback method to be invoked when the view tree is about to be drawn.
     * At this point, all views in the tree have been measured and given a frame.
     * So calculate the bounds before rendering the layout.
     */
//    private fun init() {
//        this.viewTreeObserver.addOnPreDrawListener(ViewTreeObserver.OnPreDrawListener {
//            if (width != height) {
//                //Get the smaller dimension of height and width
//                val squareSize = Math.min(width, height)
//
//                //Set the dimensions to a Square
//                val lp = layoutParams
//                lp.width = squareSize
//                lp.height = squareSize
//                requestLayout()
//                return@OnPreDrawListener false
//            }
//            true
//        })
//    }

}